<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" v-auth="'contract.add'" @click="add"></el-button>
				<el-button type="danger" plain icon="el-icon-delete" v-auth="'contract.delete'" :disabled="selection.length == 0" @click="batch_del"
						  ></el-button>
				<el-button type="warning" plain v-auth="'contract.deliver'" :disabled="selection.length == 0" @click="batch_deliver">批量发货</el-button>
				<el-button type="warning" plain v-auth="'contract.invoice'" :disabled="selection.length == 0" @click="batch_invoice">批量开票</el-button>
				<el-button type="warning" plain v-auth="'contract.invoice'" :disabled="selection.length == 0" @click="batch_receive">批量付款</el-button>
				<sc-file-import :apiObj="$API.procure.order.import" :type="1" @success="success" ref="importBox" :templateUrl="`${config.API_URL}/order.xlsx`" v-if="showImpoet"></sc-file-import>
				<sc-check-button :auth="'contract.check'" :apiObj="checkObj" @checkSuccess="checkSuccess" :selection="selection"></sc-check-button><!--审核-->
				<sc-unCheck-button :auth="'contract.uncheck'" :apiObj="unCheckObj" @checkSuccess="checkSuccess" title="确定反审核吗?" :selection="selection"></sc-unCheck-button><!--反审核-->
				<sc-export-button :auth="'contract.export'" :apiObj="apiObj" :search="search"></sc-export-button>
				<sc-print-button :auth="'contract.print'" :selection="selection"></sc-print-button><!-- 打印-->
				<!-- <el-button type="primary" icon="el-icon-plus" v-auth="'contract.add'" @click="openExpend">展开</el-button> -->
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
                    <el-select v-model="search.status" filterable placeholder="审核状态" clearable>
						<el-option label="待审核" :value="0"/>
						<el-option label="审核通过" :value="1"/>
						<el-option label="审核驳回" :value="2"/>
					</el-select>
                    <el-select v-model="search.send_status" filterable placeholder="送货状态" clearable>
						<el-option label="未发货" :value="0"/>
						<el-option label="已发货" :value="1"/>
					</el-select>
                    <el-select v-model="search.is_ticket" filterable placeholder="开票状态" clearable>
						<el-option label="未开票" :value="1"/>
						<el-option label="已开票" :value="2"/>
					</el-select>
                </div>
			</div>
		</el-header>
		<el-header>
			<div class="left-panel">
                <el-button type="primary" :icon="!isExpend?'el-icon-ArrowRight':'el-icon-ArrowDown'" v-auth="'contract.add'" @click="zhankai">一键{{!isExpend? '展开' : '收起'}}</el-button>
            </div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-date-picker v-model="search.open_single" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"/>
					<el-input v-model="search.contract_code" placeholder="请输入采购单号" clearable @keyup.enter="upsearch"></el-input>
					<el-input v-model="search.apply_name" placeholder="请输入供应商名称" clearable @keyup.enter="upsearch"></el-input>
					<el-input v-model="search.goods_name" placeholder="请输入商品名称/商品规格" clearable @keyup.enter="upsearch"></el-input>
					<el-input v-model="search.remark" placeholder="备注" clearable @keyup.enter="upsearch"></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					<el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border @row-dblclick="table_edit" tableName="produceTable" :column="defaultColumn" :isExpend="isExpend">
				<el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
				<el-table-column type="index" label="序号" fixed align="center" width="50"></el-table-column>
                <el-table-column label="明细表" type="expand" prop="expand" align="center" width="70">
                    <template #default="props">
                        <div m="4">
                            <scTable :data="props.row.product" hidePagination hideDo border>
                                <el-table-column width="150" />
                                <el-table-column label="商品名称" prop="goods.goods_name" width="150" />
                                <el-table-column label="规格" prop="goods.goods_spec" width="150" />
                                <el-table-column label="数量" prop="sale_num" width="150" />
                                <el-table-column label="价格" prop="price" width="150" />
                                <el-table-column label="金额" prop="total_price" width="150" />
                                <el-table-column label="备注" prop="remark" width="150" />
                                <el-table-column label="订单号码" prop="order_number" width="150" />
                                <el-table-column label="修改日期" prop="update_time" width="150" />
                                <el-table-column label="备注2" prop="remark2" width="150" />
                            </scTable>
                        </div>
                    </template>
                </el-table-column>
                <template #apply_name="scope">
                    {{scope.row.apply && scope.row.apply.apply_name}}
				</template>
                <template #status="scope">
                    <el-tag v-if="scope.row.status == 0" type="warning">{{ scope.row.status_text }}</el-tag>
                    <el-tag v-if="scope.row.status == 1" type="success">{{ scope.row.status_text }}</el-tag>
                    <el-tag v-if="scope.row.status == 2" type="danger">{{ scope.row.status_text }}<span v-if="scope.row.reason">：</span>{{ scope.row.reason }}</el-tag>
                </template>
                <template #send_status="scope">
                    <el-tag type="danger" v-if="scope.row.send_status == 0"> 未发货</el-tag>
                    <el-tag type="success" v-if="scope.row.send_status == 1">已发货</el-tag>
                </template>
                <template #is_ticket="scope">
                    <el-tag :type="scope.row.is_ticket == 2 ? 'primary' : 'danger'">
                        {{ scope.row.is_ticket == 2 ? '已开票' : '未开票' }}
                    </el-tag>
                </template>
                <template #is_receive="scope">
                    <el-tag :type="scope.row.is_receive == 1 ? 'primary' : 'danger'">
                        {{ scope.row.is_receive == 1 ? '已付款' : '未付款' }}
                    </el-tag>
                </template>
                <template #remark="scope">
                    <el-popover placement="top-start" :width="200" trigger="hover" :content="scope.row.remark && scope.row.remark">
                        <template #reference>
                            <el-button style="padding: 5px 0;font-size: 12px" text class="m-2">{{ scope.row.remark && scope.row.remark.substring(0, 10) }}</el-button>
                        </template>
                    </el-popover>
                </template>
				<el-table-column label="操作" fixed="right" align="center" width="180">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'contract.show'">查看</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'contract.edit'" v-if="scope.row.status != 1">编辑</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="danger" size="small" v-auth="'contract.delete'" v-if="scope.row.status != 1">删除</el-button>
								</template>
							</el-popconfirm>
							<el-popconfirm title="确定作废吗?" @confirm="table_cancel(scope.row)">
								<template #reference>
									<el-button text type="info" size="small" v-auth="'contract.cancel'" v-if="scope.row.status == 1 && scope.row.contract_status == 0">作废</el-button>
								</template>
							</el-popconfirm>
							<el-button text type="warning" size="small" @click="table_production(scope.row, scope.$index)" v-auth="'contract.production'" v-if="scope.row.status == 1 && scope.row.contract_status != 2">发起生产</el-button>
							<el-button text type="warning" size="small" @click="table_copy(scope.row, scope.$index)">复制订单</el-button>
							<el-button text type="success" size="small" @click="table_go_plan(scope.row, scope.$index)" v-auth="'contract.show'" v-if="scope.row.status == 1 && scope.row.contract_status != 2">发起采购计划</el-button>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
	<save-production v-if="dialog.production" ref="productionDialog" @success="handleSuccess" @closed="dialog.production = false"></save-production>
	<plan-save v-if="dialog.plan" ref="planDialog" @success="handleSuccess" @closed="dialog.plan = false"></plan-save>
</template>

<script>
import SaveProduction from '@/views/production/plan/save.vue'
import saveDialog from './save'
import planSave from '@/views/procure/plan/save.vue'
import config from "@/config"
export default {
	name: 'BOM列表',
	components: {
		saveDialog, SaveProduction, planSave
	},
	data() {
		return {
			dialog: {
				save: false,
				production: false,
				plan: false,
			},
			apiObj: this.$API.procure.order.list,
			shopObj: this.$API.procure.order.detailList,
			unCheckObj: this.$API.procure.order.uncheckStatus,
			checkObj: this.$API.procure.order.check,
			selection: [],
			search: {
				open_single: null,
				contract_code: null,
				apply_name: null,
				goods_name: null,
				status: null,
				remark:null
			},
			config:config,
			showImpoet: this.$TOOL.data.get("PERMISSIONS").includes('contract.import'),
            edit:this.$TOOL.data.get("PERMISSIONS").includes('contract.edit'),
            defaultColumn: [
                {label: "采购单号",prop: "contract_code",width: "125" ,fixed: "left"},
				// {label: "明细表",prop: "expand",width: "90"},
				{label: "开单日期",prop: "open_single",width: "170"},
				{label: "供应商",prop: "apply_name",width: "140"},
				{label: "发货日期",prop: "send_date",width: "100"},
				{label: "采购金额",prop: "contract_price",width: "90"},
				{label: "采购数量",prop: "order_goods_number",width: "90"},
				{label: "制单人",prop: "create_staff_name",width: "80"},
				{label: "送货状态",prop: "send_status",width: "100"},
				{label: "开票状态",prop: "is_ticket",width: "100"},
				{label: "付款状态",prop: "is_receive",width: "100"},
				{label: "审核状态",prop: "status",width: "100"},
				{label: "备注",prop: "remark",width: "120"},
				{label: "创建时间",prop: "create_time",width: "150"},
				{label: "操作人",prop: "update_staff_name",width: "125"},
            ],
            isExpend: false
		}
	},
	mounted() {
	},
	methods: {
        zhankai(){
            if(!this.isExpend){
                this.isExpend = true
            }else{
                this.isExpend = false
            }
            this.$refs.table.refresh()
        },
		success(res){
			if(res.code==1){
				this.$refs.table.refresh()
				this.$refs.importBox.close()
				this.$message.success("导入成功")
			} else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		/**审核成功事件*/
		checkSuccess() {
			this.$refs.table.refresh()
		},
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			if(this.edit && row.status != 1){
                this.dialog.save = true
                this.$nextTick(() => {
                    this.$refs.saveDialog.open('edit').setData(row)
                })
            }else{
                this.$alert("没有编辑权限", "提示", {type: 'error'})
            }
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		//作废
		async table_cancel(row) {
			var reqData = {id: [row.id], status: 0}
			var res = await this.$API.procure.order.cancel.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		// 生产
		table_production(row) {
			this.dialog.production = true
			this.$nextTick(() => {
				this.$refs.productionDialog.open('production').goProduction(row)
			})
		},
		// 发起采购计划
		table_go_plan(row) {
			this.dialog.plan = true
			this.$nextTick(() => {
				this.$refs.planDialog.open('sale').saleCome(row)
			})
		},
		/**批量发货*/
		batch_deliver() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`确定将选中的 ${this.selection.length} 项发货吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				var reqData = {id: delete_ids}
				var res = await this.$API.procure.order.batchSend.post(reqData);
				if (res.code == 1) {
					this.$refs.table.refresh()
					this.$message.success("发货成功")
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}).catch(() => {

			})
		},
		/**批量开票*/
		batch_invoice() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`确定将选中的 ${this.selection.length} 项开票吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				var reqData = {id: delete_ids}
				var res = await this.$API.procure.order.batchTicket.post(reqData);
				if (res.code == 1) {
					this.$refs.table.refresh()
					this.$message.success("开票成功")
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}).catch(() => {

			})
		},
        // 批量付款
        batch_receive() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`确定将选中的 ${this.selection.length} 项付款吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				var reqData = {id: delete_ids}
				var res = await this.$API.procure.order.batchReceive.post(reqData);
				if (res.code == 1) {
					this.$refs.table.refresh()
					this.$message.success("付款成功")
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}).catch(() => {

			})
		},
		//删除
		async table_del(row, index) {
			var reqData = {id: row.id}
			var res = await this.$API.procure.order.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.tableData.splice(index, 1);
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async table_del_all(delete_ids) {
			var reqData = {id: delete_ids}
			var res = await this.$API.procure.order.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		refresh() {
			for (const key in this.search) {
				this.search[key] = null;
			}
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'production') {
				this.$refs.table.refresh()
			} else if (mode == 'sale') {
				this.$refs.table.refresh()
			} else if (mode == 'copy') {
				this.$refs.table.refresh()
			}
		},
		async table_copy(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('copy').setData(row, 1)
			})
		},
	}
}
</script>

<style></style>
